#include "server_function.h"


//普通用户登录
void my_up(MSG *msg,sqlite3 *db,int clifd)
{
    printf("进入用户登录界面");
    char sql[128]={0};
    char ** res;//查询结果
    int m=0;//行数
    int n=0;//列数
    char * error;
    sprintf(sql,"select * from user where id = '%s' and password = '%s';",msg->id,msg->password);
    printf ("%s",sql);
    if (sqlite3_get_table(db,sql,&res,&m,&n,&error) != 0)
    {
        printf("没有查询结果");
        strcpy(msg->cw,"无此成员");   
    }
    if(m>0)//说明查询有结果
    {
        printf("有查询结果");
        memset(msg,0,sizeof(msg));
        strcpy(msg->cw,"ok");
    }
    if (send(clifd,msg,sizeof(MSG),0) <0)
    {
        perror("send");
        exit(-1);   
    }
    
    time_t t = time(NULL);
    struct tm *tp;
    tp = localtime(&t);
    char time_c[128] = "";
    sprintf(time_c,"%d-%d-%d %d:%d:%d",(tp->tm_year) + 1900,(tp->tm_mon)+1,tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);
    printf("%s\n",time_c);
    sprintf(sql,"insert into user_history values('%s','%s');",msg->id,time_c);
    if(sqlite3_exec(db,sql,NULL,NULL,&error)!= SQLITE_OK){
        fprintf(stderr,"插入:%s\n",error);
        sqlite3_close(db);

    }else
        puts("插入成功\n");



}

//普通用户注册
void my_in(MSG *msg,sqlite3 *db,int clifd)
{
    char *error;
    char sql[128]={0};
    //封装语句
    sprintf(sql,"insert into user values('%s','%s','%s',%d,'%s','%s',%d);",msg->id,msg->password,msg->name,msg->age,msg->gender,msg->phone,msg->wage);
    printf("调试语句：%s\n",sql) ;
    if (sqlite3_exec(db,sql,NULL,NULL,&error) != SQLITE_OK)
    {
        printf("%s\n",error);
        strcpy(msg->cw,"err");
    }
    else 
    {
        printf("用户注册成功");
        strcpy(msg->cw,"ok");    
    }

    if(send(clifd,msg,sizeof(MSG),0) < 0)
    {
        printf("发送失败");
    }
}

//管理员登录
void root_up(MSG*msg,sqlite3 *db,int clifd)
{
    char * err;
    char sql[128]={0};
    int n,m;
    char ** res;
    sprintf(sql,"select * from root where user ='%s' and password='%s';",msg->id,msg->password);
    if(sqlite3_get_table(db,sql,&res,&m,&n,&err)!=0)
    {
        printf("没有查询到结果\n");
        strcpy(msg->cw,"无此成员");
    }
    if(m>0)
    {
        printf("有查询结果\n");
        memset(msg,0,sizeof(msg));
        strcpy(msg->cw,"ok");
    }
    if (send(clifd,msg,sizeof(MSG),0) < 0)
    {
        printf("发送失败\n");
    }

}
int callback_history(void *para,int f_num,char **f_value,char **f_name)
{
    int clifd;
    clifd = *((int *)para);
    printf("clifd = %d\n",clifd);
    memset(&info_meg,0,sizeof(INFO));
    strcpy(info_meg.id,f_value[0]);
    strcpy(info_meg.login_info,f_value[1]);
    printf("%s ",info_meg.id);
    printf("%s \n",info_meg.login_info);
    if(send(clifd,&info_meg,sizeof(INFO),0) < 0){
        perror("sendend");
        return -1;
    }else
    {
        printf("---------\n");
    }
    return 0;

}
//历史查询
void my_history(MSG *msg,sqlite3 *db,int clifd)
{
    char *error;
    char sql[123] = {0};
    sprintf(sql,"select * from user_history where id='%s';",msg->id);
    if(sqlite3_exec(db,sql,callback_history,&clifd,&error)!= SQLITE_OK){
        printf("没有查询到结果\n");

    }else{
        puts("查询成功");


    }
    strcpy(info_meg.cw,"quit");
    if(send(clifd,&info_meg,sizeof(INFO),0) < 0){
        perror("send");
    }else{
        printf("quit\n");
    }

}


//信息查询
void my_message(MSG *msg,sqlite3 *db,int clifd)
{
    char *error;

    char sql[123]={0};
    int n=0;
    int m=0;
    char **res;
    sprintf(sql,"select * from user where id='%s';",msg->id);
    if (sqlite3_get_table(db,sql,&res,&m,&n,&error) != SQLITE_OK)
    {
        printf("没有查询结果");
        strcpy(msg->cw,"无此成员");   
    }
    if(m>0)//说明查询有结果
    {
        memset(msg,0,sizeof(MSG));
        printf ("进入提取模块\n");
        strcpy(msg->cw,"ok");
        //  int i =0;
        int nn=n;
        //   for (i=0;i<n;i++)
        //   {
        //  if(i = 0)
        strcpy(msg->id,res[nn++]);//取出用户名
        //   if(i = 1)i
        printf("===========%s\n",msg->id);
        strcpy(msg->password,res[nn++]);//取出密码
        printf("===========%s\n",msg->password);
        //   if(i = 2)
        strcpy(msg->name,res[nn++]);//取出姓名
        printf("===========%s\n",msg->name);
        //   if(i = 3)
        msg->age = atoi(res[nn++]);//取出年龄
        printf("===========%d\n",msg->age);
        //  if(i = 4)
        strcpy(msg->gender ,res[nn++]);//取出性别
        printf("===========%s\n",msg->gender);
        //  if(i = 5)
        strcpy( msg->phone ,res[nn++]);//取出手机号
        printf("===========%d\n",msg->age);
        //  if(i = 6)
        msg->wage = atoi(res[nn++]);//取出工资
        printf("===========%d\n",msg->wage);
    }
    printf("------------------发送----------\n");
    if (send(clifd,msg,sizeof(MSG),0) <0)
    {
        perror("send");


    }
}

//信息修改
void my_amend(MSG *msg,sqlite3 *db,int clifd)
{
    char *err;
    char sql[123];
    if(msg->a ==0)
    {
        sprintf(sql,"update user set '%s'='%s'where id='%s';",msg->cw,msg->jj,msg->id);
    }
    else if(msg->a ==1)
    {
        sprintf(sql,"update user set '%s'=%d where id='%s';",msg->cw,msg->ii,msg->id); 
    }
    if(sqlite3_exec(db,sql,NULL,NULL,&err) != SQLITE_OK)
    {
        printf("修改失败\n");
        strcpy(msg->cw,"err");
    }
    else
    {
        strcpy(msg->cw,"ok");
    }
    if (send(clifd,msg,sizeof(MSG),0) < 0)
    {
        printf("发送失败\n");
    }

}

//新增成员
void my_new(MSG *msg,sqlite3 *db,int clifd)
{
    char *err;
    char sql[123]={0};

    sprintf(sql,"insert into user values('%s','%s','%s',%d,'%s','%s',%d);",msg->id,msg->password,msg->name,msg->age,msg->gender,msg->phone,msg->wage);
    printf("调试语句：%s\n",sql) ;
    if (sqlite3_exec(db,sql,NULL,NULL,&err) != SQLITE_OK)
    {
        printf("%s\n",err);
        strcpy(msg->cw,"用户名存在");
    }
    else 
    {
        printf("用户注册成功");
        strcpy(msg->cw,"ok");    
    }

    if(send(clifd,msg,sizeof(MSG),0) < 0)
    {
        printf("发送失败");
    }
}
//删除成员
void my_del(MSG *msg,sqlite3 *db,int clifd)
{
    char * err;
    char sql[123]={0};
    sprintf(sql,"delete from user where name='%s';",msg->id);
    if( sqlite3_exec(db,sql,NULL,NULL,&err)!= SQLITE_OK)
    {
        strcpy(msg->cw,"error");
    }
    else
    {
        strcpy(msg->cw,"ok");

    }
    if (send(clifd,msg,sizeof(MSG),0) < 0)
    {
        printf("发送失败\n");
    }
} 
//密码修改
void my_password(MSG *msg,sqlite3 *db,int clifd)
{
    char *err;
    char sql[123];
    sprintf(sql,"update user set password='%s' where id='%s';",msg->cw,msg->id);
    printf("调试语句%s\n",sql);
    if(sqlite3_exec(db,sql,NULL,NULL,&err) != SQLITE_OK)
    {
        printf("修改失败\n");
        strcpy(msg->cw,"err");
    }
    else
    {
        strcpy(msg->cw,"ok");
    }
    if (send(clifd,msg,sizeof(MSG),0) < 0)
    {
        printf("发送失败\n");
    }

}
